package il.ac.bgu.cs.heuristics.graph.pddl.parser;

import il.ac.bgu.cs.heuristics.graph.ActionEdge;
import il.ac.bgu.cs.heuristics.graph.Path;
import il.ac.bgu.cs.heuristics.graph.pddl.auxiliary.ContradictionResolver;
import il.ac.bgu.cs.heuristics.graph.types.IState;
import il.ac.bgu.cs.heuristics.graph.types.ISubstitution;
import il.ac.bgu.cs.heuristics.utils.CollectionUtils;

import java.io.InputStream;
import java.util.Set;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.junit.Test;

/**
 * @author haro
 * 
 */
public class PathImplTester extends TestCase {
	@Test
	public void testSimpleShortPath() {
		final InputStream in = ContradictionTests.class
				.getResourceAsStream(TestUtils.NO_CONTRADICTIONS_FILE_NAME);
		// the contradiction file is empty
		try {
			final ContradictionResolver resolver = new ContradictionResolver(in);
			final ISubstitution sub1Node1 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X1);
			final ISubstitution sub2Node1 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X2);
			IState node1 = TestUtils.createState(sub1Node1, sub2Node1);

			final ISubstitution sub1Node2 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X1);
			final ISubstitution sub2Node2 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X2);
			IState node2 = TestUtils.createState(sub1Node2, sub2Node2);

			final ISubstitution sub1Node3 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X3);
			IState node3 = TestUtils.createState(sub1Node3);

			final ISubstitution sub1Node4 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X3);
			IState node4 = TestUtils.createState(sub1Node3);

			// actual value
			Path path = TestUtils.createPath(
					new ActionEdge(node1, node2, false), new ActionEdge(node2,
							node3, true), new ActionEdge(node3, node4, false));
			Set<ISubstitution> actualConstrains = path.getConstraints();

			// expected value
			ISubstitution sub = TestUtils.createSubstitution(resolver,
					TestUtils.IS_TRUE, TestUtils.X3);

			Set<ISubstitution> expectedConstrains = CollectionUtils
					.newLinkedHashSet();
			expectedConstrains.add(sub);

			// test
			Assert.assertTrue(actualConstrains.equals(expectedConstrains));

		} catch (Exception e) {
		}

	}

	public void testGetEndState1() {
		final InputStream in = ContradictionTests.class
				.getResourceAsStream(TestUtils.NO_CONTRADICTIONS_FILE_NAME);
		// the contradiction file is empty
		try {
			final ContradictionResolver resolver = new ContradictionResolver(in);

			final ISubstitution sub1Node1 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X1);
			IState node1 = TestUtils.createState(sub1Node1);

			final ISubstitution sub1Node2 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X1);
			IState node2 = TestUtils.createState(sub1Node2);

			final ISubstitution sub1Node3 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X2);
			final ISubstitution sub2Node3 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X3);
			IState node3 = TestUtils.createState(sub1Node3, sub2Node3);

			final ISubstitution sub1Node4 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X2);
			IState node4 = TestUtils.createState(sub1Node4);

			final ISubstitution sub1Node5 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X2);
			final ISubstitution sub2Node5 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X3);
			IState node5 = TestUtils.createState(sub1Node5, sub2Node5);

			// actual value
			Path path = TestUtils.createPath(
					new ActionEdge(node1, node2, false), new ActionEdge(node2,
							node3, true), new ActionEdge(node3, node4, false),
					new ActionEdge(node4, node5, true));
			IState endState = path.getExtendedGoalState();

			// expected value
			ISubstitution sub1 = TestUtils.createSubstitution(resolver,
					TestUtils.IS_FALSE, TestUtils.X1);
			ISubstitution sub2 = TestUtils.createSubstitution(resolver,
					TestUtils.IS_FALSE, TestUtils.X2);
			ISubstitution sub3 = TestUtils.createSubstitution(resolver,
					TestUtils.IS_FALSE, TestUtils.X3);
			IState expectedResult = TestUtils.createState(sub1, sub2, sub3);

			// test
			Assert.assertTrue(endState.equals(expectedResult));

		} catch (Exception e) {
		}

	}

	public void testGetEndState2() {
		final InputStream in = ContradictionTests.class
				.getResourceAsStream(TestUtils.NO_CONTRADICTIONS_FILE_NAME);
		// the contradiction file is empty
		try {
			final ContradictionResolver resolver = new ContradictionResolver(in);

			final ISubstitution sub1Node1 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X1);
			IState node1 = TestUtils.createState(sub1Node1);

			final ISubstitution sub1Node2 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X1);
			IState node2 = TestUtils.createState(sub1Node2);

			final ISubstitution sub1Node3 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X1);
			final ISubstitution sub2Node3 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X3);
			IState node3 = TestUtils.createState(sub1Node3, sub2Node3);

			final ISubstitution sub1Node4 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_FALSE, TestUtils.X2);
			IState node4 = TestUtils.createState(sub1Node4);

			final ISubstitution sub1Node5 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X1);
			final ISubstitution sub2Node5 = TestUtils.createSubstitution(
					resolver, TestUtils.IS_TRUE, TestUtils.X3);
			IState node5 = TestUtils.createState(sub1Node5, sub2Node5);

			// actual value
			Path path = TestUtils.createPath(
					new ActionEdge(node1, node2, false), new ActionEdge(node2,
							node3, true), new ActionEdge(node3, node4, false),
					new ActionEdge(node4, node5, true));
			IState endState = path.getExtendedGoalState();

			// expected value
			ISubstitution sub1 = TestUtils.createSubstitution(resolver,
					TestUtils.IS_TRUE, TestUtils.X1);
			ISubstitution sub2 = TestUtils.createSubstitution(resolver,
					TestUtils.IS_FALSE, TestUtils.X2);
			ISubstitution sub3 = TestUtils.createSubstitution(resolver,
					TestUtils.IS_TRUE, TestUtils.X3);
			IState expectedResult = TestUtils.createState(sub1, sub2, sub3);

			// test
			Assert.assertTrue(endState.equals(expectedResult));

		} catch (Exception e) {
		}

	}
}
